package com.knowswift.issues.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.knowswift.common.bean.project.po.ModulePermission;
import com.knowswift.common.bean.project.vo.ModulePermissionVO;
import com.knowswift.common.bean.project.vo.ProjectLimit;
import com.knowswift.common.utils.WrappedBeanCopier;
import com.knowswift.issues.mapper.ModulePermissionMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class ModulePermissionService extends ServiceImpl<ModulePermissionMapper, ModulePermission> {
    public void concatData(List<ModulePermissionVO> list) {
        List<ProjectLimit> projectLimits;
        List<ModulePermissionVO> list2 = new ArrayList<>();
        for (ModulePermissionVO permissionVO : list) {
            projectLimits = WrappedBeanCopier.JSONArrayToList(permissionVO.getProjects(), ProjectLimit.class);
            List<ProjectLimit> list1 = projectLimits.stream().filter(projectLimit -> Boolean.TRUE.equals(projectLimit.getLimit())).collect(Collectors.toList());
            if (list1.isEmpty()) {
                continue;
            }
            projectLimits = list1.stream().sorted(Comparator.comparing(ProjectLimit::getProjectOrder)).collect(Collectors.toList());
            permissionVO.setProjects(projectLimits);
            list2.add(permissionVO);
        }
        list.clear();
        list.addAll(list2);
    }
}